package com.devmindset.legoframework.client.mvc;

import com.google.gwt.user.client.Cookies;

/**
 * Language native service class. Offers services that can
 * be used in the internationalization process of the
 * application.
 *  
 * @author <a href="ice.penov@devmindset.com">Ice Penov</a>
 */
public final class LanguageService {
  
	/**
	 * Native service class. Static usage only.
	 */
  private LanguageService() {}
  
  public static final String COOKIE_NAME = "legoframework_language";
  
  /**
   * Sets the language of the application.<br/><br/> 
   * Basically, sets the {@link LanguageService#COOKIE_NAME}
   * with the langCode value.
   * 
   * @param langCode language code
   */
  public static void toLanguage(String langCode) {
    Cookies.setCookie(COOKIE_NAME, langCode);
  }
  
  public static void removeLanguageCookie() {
  	Cookies.removeCookie(COOKIE_NAME);
  }
  
  /**
   * Checks whether the application is currently in the
   * language given by its language code.
   * 
   * @param langCode the language code
   * @return the boolean result; true if set, false otherwise
   */
  public static native boolean isLanguage(String langCode) /*-{
	  if ($wnd.location.toString().match("locale=" + [langCode])) {
	  	return true;
	  } else {
	  	return false;
	  }
  }-*/;
  
  public static native String getCurrentLanguage(String defaultLanguage) /*-{
    if ($wnd.location.toString().match("locale=" + [langCode])) {
      var start = $wnd.location.toString().indexOf("locale=");
      var langCode = $wnd.location.toString().substring(start + 7, start + 9);
      return langCode;
    } else {
    	return defaultLanguage;
    } 
  }-*/;
    
  public static native void appendLocale() /*-{
	  var langCode = null;
	  var nameEQ = "legoframework_language=";
	  var ca = document.cookie.split(';');
	  for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) langCode = c.substring(nameEQ.length,c.length);
	  }
	if (langCode) {
	  var hasLocale = false;
	  if ($wnd.location.toString().match("locale=" + langCode)) {
	    hasLocale = true;
	  }
	  if (!hasLocale) {
	  
	  	var withoutLocale = "";
	  	var base;
	  	var parts;
	  	var parts1 = $wnd.location.toString().split("?");
	  	if (parts1.length == 1) {
	  	  $wnd.location.href = $wnd.location.toString() + "?locale=" + langCode;
	  	  return;
	  	} else {
	  	  base = parts1[0];
	  	  if (parts1[1].match("&")) {
	  	    parts = parts1[1].split("&");
	  	  } else {
	  	    parts = parts1[1].split("?");
	  	  }
	  	}	  	
	  	var flag = true;
	  	var second = "";
	  	for(i in parts) {
	  	  if (!parts[i].match('locale=')) {
		  		if (flag) {
		  		  second = second + "?" + parts[i];
		  		  flag = false;
		  		} else {
		  		  second = second + "&" + parts[i];
		  		}
		  	}
	  	}
	    
	    var clean = base + second;
	    clean = clean.replace("#", "");
						
		  var concatSymbol;
		  var currLocation = clean.split("?"); 
		  if (currLocation.length == 1) {
		    concatSymbol = "?";
		  } else {
		    concatSymbol = "&";
		  }
		    var currLocale = concatSymbol + "locale=" + langCode;
		    $wnd.location.href = clean + currLocale;	    
	    }
	}
}-*/;
 
}